package com.mq.config6_dlx;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 普通队列： normal.queue
 * 普通交换机： normal.exchange
 * 队列绑定到交换机： city.gz
 * <p>
 * 死信队列： dlx.queue
 * 死信交换机： dlx.exchange
 * 死信队列绑定到死信交换机： dlx.routing.key
 */
@Configuration
public class DeadLetterConfig {

    /**
     * 普通队列配置
     */
    @Bean
    public Queue normalQueue() {
        return QueueBuilder.durable("normal.queue")
                .deadLetterExchange("dlx.exchange")  // 配置普通队列绑定死信交换机
                .deadLetterRoutingKey("dlx.routing.key") // 配置普通队列绑定死信路由key
                //.ttl(10000)  // 队列中消息10秒没有被消费，就过期，自动投递到私信队列
                //.maxLength(2)
                .build();
    }
    @Bean
    public DirectExchange normalExchange() {
        return new DirectExchange("normal.exchange");
    }
    @Bean
    public Binding normalQueueToExchange(
            @Qualifier("normalQueue") Queue normalQueue,
            @Qualifier("normalExchange") DirectExchange normalExchange) {
        return BindingBuilder.bind(normalQueue).to(normalExchange).with("city.gz");
    }

    /**
     * 死信队列配置
     */
    @Bean
    public Queue dlxQueue() {
        return new Queue("dlx.queue");
    }
    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange("dlx.exchange");
    }
    @Bean
    public Binding dlxQueueToExchange(
            @Qualifier("dlxQueue") Queue dlxQueue,
            @Qualifier("dlxExchange") DirectExchange dlxExchange) {
        return BindingBuilder.bind(dlxQueue).to(dlxExchange).with("dlx.routing.key");
    }
}

















